package com.ydd.controller.admin;


import com.ydd.context.BaseContext;
import com.ydd.dto.ArticleAddDTO;
import com.ydd.dto.ArticleListDTO;
import com.ydd.dto.ArticleUpdateDTO;
import com.ydd.entity.Article;
import com.ydd.result.Result;
import com.ydd.service.IArticleService;
import com.ydd.vo.ArticleListVO;
import com.ydd.vo.ArticleRowInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hgz
 * @since 2023-05-30
 */
@Slf4j
@Api(tags = "文章管理")
@RestController("adminArticleController")
@RequestMapping("/admin/ydd_article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    /**
     * 分页列表查询
     *
     * @param articleListDTO
     * @return
     */
    @ApiOperation("分页列表")
    @GetMapping("/list")
    public Result list(ArticleListDTO articleListDTO) {
        log.info("{}", articleListDTO);
        ArticleListVO articleListVO = articleService.pageList(articleListDTO);
        return Result.success(articleListVO);
    }

    @ApiOperation("根据id删除文章")
    @GetMapping("/del")
    public Result del(Integer id) {
        articleService.removeById(id);
        return Result.success();
    }

    /**
     * 文章添加
     *
     * @return
     */
    @ApiOperation("文章添加")
    @PostMapping("/add")
    public Result add(@RequestBody ArticleAddDTO articleAddDTO) {
        articleService.add(articleAddDTO);
        return Result.success();
    }

    /**
     * 根据id查询文章
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    @ApiOperation("根据id查询文章")
    public Result rowInfo(Integer id) {
        Article byId = articleService.getById(id);
        ArticleRowInfoVO articleRowInfoVO = new ArticleRowInfoVO();
        BeanUtils.copyProperties(byId,articleRowInfoVO);
        articleRowInfoVO.setCreate_Date(byId.getCreateDate());
        articleRowInfoVO.setUpdate_Date(byId.getUpdateDate());
        return Result.success(articleRowInfoVO);
    }


    @ApiOperation("修改文章")
    @PostMapping("/update")
    public Result update(@RequestBody ArticleUpdateDTO articleUpdateDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(articleUpdateDTO,article);
        article.setUpdateDate(System.currentTimeMillis());
        articleService.updateById(article);
        return Result.success();
    }

}
